package com.augurit.common.syslog.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.entity.SysLoginLog;
import com.augurit.common.syslog.web.form.SysLoginLogForm;


public class SysLoginLogConvertor {

	public static SysLoginLogForm convertVoToForm(SysLoginLog entity) {
		if(entity != null) {
			SysLoginLogForm form = new SysLoginLogForm();
			form.setId(entity.getId());
			form.setUserName(entity.getUserName());
			form.setLoginTime(entity.getLoginTime());
			form.setClientIp(entity.getClientIp());
			String loginState = "";
			if(entity.getLoginState() != null){
				loginState = entity.getLoginState().equals("Y")?"登录成功":"登录失败";
			}
			
			form.setLoginState(loginState);
			form.setLoginMessage(entity.getLoginMessage());
			form.setMemo(entity.getMemo());
			return form;
		}else
			return null;
	}
	
	public static void convertFormToVo(SysLoginLogForm form, SysLoginLog entity) {
		if(entity != null && form != null) {
			entity.setId(form.getId());
			if(form.getUserName() != null && form.getUserName().trim().length() > 0)
				entity.setUserName(form.getUserName().trim());
			if(form.getLoginTime() != null){
				entity.setLoginTime(form.getLoginTime());
			}
			if(form.getClientIp() != null && form.getClientIp().trim().length() > 0){
				entity.setClientIp(form.getClientIp());
			}
			if(form.getLoginState() != null && form.getLoginState().length() > 0){
				entity.setLoginState(form.getLoginState());
			}
			if(form.getLoginMessage() != null && form.getLoginMessage().trim().length() > 0){
				entity.setLoginMessage(form.getLoginMessage());
			}
			if(form.getMemo() != null && form.getMemo().trim().length() > 0){
				entity.setMemo(form.getMemo());
			}
		}
	}
	
	public static List<SysLoginLogForm> convertVoListToFormList(List<SysLoginLog> sysLoginLogList) {
		if(sysLoginLogList != null && sysLoginLogList.size() > 0) {
			List<SysLoginLogForm> sysLoginLogFormList = new ArrayList();
			for(int i=0; i<sysLoginLogList.size(); i++) {
				sysLoginLogFormList.add(convertVoToForm(sysLoginLogList.get(i)));
			}
			return sysLoginLogFormList;
		}
		return null;
	}
	
	public static List<SysLoginLog> convertFormListToVoList(List<SysLoginLogForm> sysLoginLogFormList) {
		if(sysLoginLogFormList != null && sysLoginLogFormList.size() > 0) {
			List<SysLoginLog> sysLoginLogList = new ArrayList();
			for(int i=0; i<sysLoginLogFormList.size(); i++) {
				SysLoginLog sysLoginLog = new SysLoginLog();
				convertFormToVo(sysLoginLogFormList.get(i), sysLoginLog);
				sysLoginLogList.add(sysLoginLog);
			}
			return sysLoginLogList;
		}
		return null;
	}
}
